package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.PeriodicalsDb;
import com.company.project.service.PeriodicalsDbService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by CodeGenerator on 2018/10/08.
*/
@RestController
@RequestMapping("/periodicals/db")
public class PeriodicalsDbController {
    @Resource
    private PeriodicalsDbService periodicalsDbService;

    @PostMapping("/add")
    public Result add(PeriodicalsDb periodicalsDb) {
        periodicalsDbService.save(periodicalsDb);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        periodicalsDbService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(PeriodicalsDb periodicalsDb) {
        periodicalsDbService.update(periodicalsDb);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        PeriodicalsDb periodicalsDb = periodicalsDbService.findById(id);
        return ResultGenerator.genSuccessResult(periodicalsDb);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<PeriodicalsDb> list = periodicalsDbService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    //后台添加期刊获取期刊收录数据库
    @GetMapping("/admin/PeriodicalsDb/list")
    public List<PeriodicalsDb> adminGetPdP(){
        return periodicalsDbService.adminGetPdP();
    }

    /**
     * 前台查询期刊核心
     * @return
     */
    @GetMapping("/AllPeriodicalsDb")
    public List<PeriodicalsDb> AllPeriodicalsDb (){
        return periodicalsDbService.AllPeriodicalsDb();
    }

}
